Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access a Map with Primitive type keys #12259

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

dharanad
Copy link
Contributor

@dharanad dharanad commented Aug 30, 2024

Which issue does this PR close?

Partially fixes #11785

Rationale for this change

What changes are included in this PR?

Added support to access map with primite type keys.
Using compound type as key is beyond the scope of this PR

Are these changes tested?

Uncommented existing test & ran cargo test

Are there any user-facing changes?

@github-actions github-actions bot added sqllogictest SQL Logic Tests (.slt) functions labels Aug 30, 2024
@dharanad dharanad changed the title [FIX] Access a Map with a non-string keys [WIP][FIX] Access a Map with a non-string keys Aug 30, 2024
@dharanad dharanad changed the title [WIP][FIX] Access a Map with a non-string keys [WIP][FIX] Access a Map with a integer keys Aug 30, 2024
@dharanad dharanad changed the title [WIP][FIX] Access a Map with a integer keys [FIX] Access a Map with a integer keys Aug 30, 2024
@dharanad dharanad changed the title [FIX] Access a Map with a integer keys [FIX] Access a Map with Primitive keys Aug 30, 2024
@dharanad dharanad changed the title [FIX] Access a Map with Primitive keys Access a Map with Primitive keys Aug 30, 2024
@dharanad dharanad changed the title Access a Map with Primitive keys Access a Map with Primitive type keys Aug 30, 2024
@dharanad dharanad marked this pull request as ready for review August 30, 2024 17:31
@dharanad
Copy link
Contributor Author

@goldmedal Can you take a look

Copy link
Contributor

@goldmedal goldmedal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dharanad works on this. Overall LGTM. Just some minor suggestions.

datafusion/functions/src/core/mod.rs Outdated Show resolved Hide resolved
datafusion/functions/src/core/getfield.rs Outdated Show resolved Hide resolved
@dharanad
Copy link
Contributor Author

@jayzhan211 Can you please me with a review here.

SELECT MAP { 1.0: 'a', 2.0: 'b', 3.0: 'c' }[2.0];
----
b

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if SELECT MAP { 1.0: 'a', 2.0: 'b', 3.0: 'c' }[2];?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current implmentation will throw an error. Just checked DuckDB, it try to cast the index to keys data type.

----
c

# FIXME: DuckDB handles this case
Copy link
Contributor Author

@dharanad dharanad Aug 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh i found out why.

Utf8 to numeric: strings that can’t be parsed to numbers return null, float strings in integer casts return null

Ref: https://docs.rs/arrow/latest/arrow/compute/kernels/cast/fn.cast_with_options.html

Taking a look into cast.rs for further reference

Copy link
Contributor Author

@dharanad dharanad Aug 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is well documented here under Behavious section 3rd Point, that float strings in integer casts return null

> select arrow_cast('1.0', 'Int64');
Arrow error: Cast error: Cannot cast string '1.0' to value of Int64 type

let mut key_array: ArrayRef = name.to_array()?;
if key_array.data_type() != map_array.key_type() {
let pre_cast_dt = key_array.data_type().clone();
if arrow::compute::kernels::cast::can_cast_types(key_array.data_type(), map_array.key_type()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting and type check should be handled in Signature not invoke.

let Expr::Literal(name) = *index else {
return plan_err!("index should be a literal");
};
Ok(PlannerResult::Planned(get_field(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use map_extract here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map_extract returns a list containing the value. Maybe can wrap it around get_field call and extract the value. But i am not sure, let me try it out.

@alamb alamb marked this pull request as draft September 6, 2024 20:44
@alamb
Copy link
Contributor

alamb commented Sep 6, 2024

I am trying to clean up the review queue, so marking this PR as draft as the CI tests are failing. Let me know if that wasn't right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Access a Map with a non-string keys
5 participants